﻿#include"stdafx.h"
#include"SQLexpression.h"

bool SqlEx::ForCreate() {
	string msg;
	if (pSocket->cur_db == "") {
		msg = "Please Choose a database !";
		pSocket->SendMSG(msg);
		return false;
	}
	if (!authority->Auth_Judge(user_host, CStringTostring(pSocket->cur_db), "")) {
		msg = "You do not have permission to create a table !";
		pSocket->SendMSG(msg);
		return false;
	}
	vector<string>info;
	regex re1("(?<=create table ).+?(?=\\(\\D+\\))");
	regex re2("(?<=\\().+(?=\\)END)");
	smatch csm1;
	smatch csm2;
	string content;
	int pos1 = Ex.find("table");
	int pos2 = Ex.find("(");
	string tname = trimAll(Ex.substr(pos1 + 5, pos2 - pos1 - 5), " ");
	if (tname != "") {
		info.push_back(tname.c_str());
	}
	else {
		msg = "检查create table语句后是否存在错误！！！";
		pSocket->SendMSG(msg);
		return false;
	}

	if (regex_search(Ex, csm2, re2)) {
		content=trim(csm2.str(), " ");
		string field_info = "";
		vector<string>arr = split(content, ",");
		for (unsigned int i = 0; i < arr.size(); i++) {
			vector<string> temp = split(trim(arr[i]," "), " ");
			if (temp.size() == 1) {
				msg = "检查括号中语句是否存在错误！！！";
				pSocket->SendMSG(msg);
				return false;
			}
			field_info += temp[0] + " ";
			string type = "", param = "";
			if ((type = isType(temp[1], param)) == "-1") {
				msg = "不支持的类型" + temp[1];
				pSocket->SendMSG(msg);
				return false;
			}
			field_info += type + " " + param + " ";
			if (arr[i].find("primary key") != -1) {
				field_info += "6 ";
			}
			if (arr[i].find("unique") != -1) {
				field_info += "7 ";
			}
			if (arr[i].find("not null") != -1) {
				field_info += "8 ";
			}
			if (arr[i].find("auto_increase") != -1) {
				field_info += "9 ";
			}
			info.push_back(field_info.c_str());
			field_info = "";
		}
	}
	else {
		msg = "检查括号中语句是否存在错误！！！";
		pSocket->SendMSG(msg);
		return false;
	}
	serverDB->Table_Create(info, CStringTostring(pSocket->cur_db), msg);
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForDrop() {
	string msg = "";
	regex dre("(?<=drop table )\\w+(?=END)");
	smatch dsm;
	if (regex_search(Ex, dsm, dre)) {
		string str = dsm.str();
		if (!authority->Auth_Judge(user_host, CStringTostring(pSocket->cur_db), str)) {
			msg = "You do not have permission to drop the table !";
		}
		else {
			int lenth = str.length();
			char *table = new char[lenth + 1];
			memset(table, 0, lenth + 1);
			memcpy(table, str.c_str(), lenth);
			serverDB->Table_Drop(table, CStringTostring(pSocket->cur_db), msg);
			delete table;
		}
	}
	else {
		msg = "检查drop语句后是否存在错误！！！";
	}
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForDesc()
{
	string msg = "";
	if (pSocket->cur_db == "") {
		msg = "Please Choose a database !";
	}
	else {
		string db = CStringTostring(pSocket->cur_db);
		string str;
		regex reg1("(?<=desc ).+(?=END)");
		smatch sm;
		if (regex_search(Ex, sm, reg1)) {
			str = sm.str();
			if (authority->Auth_Judge(user_host, db, str)) {
				int lenth = str.length();
				char *tname = new char[lenth + 1];
				memset(tname, 0, lenth + 1);
				memcpy(tname, str.c_str(), lenth);
				serverDB->Table_desc(tname, db, msg);
				delete tname;
			}
			else {
				msg = "You do not have permission to desc the table !";
			}
		}
		else {
			msg = "检测到你的语法错误 ！";
		}
	}
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForShow()
{
	string msg = "";
	if (pSocket->cur_db == "") {
		msg = "Please Choose a database !";
	}
	else {
		string db = CStringTostring(pSocket->cur_db);
		vector<string>tables;
		serverDB->Table_show(tables, db);
		for (unsigned int j = 0; j < tables.size(); j++) {
			if (authority->Auth_Judge(user_host, db, tables[j])) {
				msg += tables[j] + "\n";
			}
		}
	}
	if (msg == "") {
		msg = "Empty database !";
	}
	pSocket->SendMSG(msg);
	return true;
}